package com.desire.homework;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author desire
 */
public class _43_画正弦曲线 {
    public static void main(String[] args) throws IOException {
        int w = 800, h = 500, t = 2;
        var i = new BufferedImage(w, h, t);
        var g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        //设置面板背景色
        g.setColor(Color.WHITE);
        //填充面板
        g.fillRect(0, 0, 400, 300);
        //设置画线的颜色
        g.setColor(Color.RED);
        double y = 0;
        double x = 0;
        //一个周期
        for (x = 0; x <= 360; x += 0.1) {
            //转化为弧度,1度=π/180弧度
            y = Math.sin(x * Math.PI / 180);
            //便于在屏幕上显示
            y = (100 + 80 * y);
            //用这种方式也可以
            g.drawString(".", (int) x, (int) y);
            //画点
            g.drawLine((int) x, (int) y, (int) x, (int) y);
        }
        ImageIO.write(i, "png", new File("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\sin.png"));
        Runtime run = Runtime.getRuntime();
        run.exec("cmd /k start C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\sin.png");
    }
}
